package com.bawei.module_live.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_live.version1.GiftEntity
import com.bawei.module_live.R
import com.bumptech.glide.Glide
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import com.opensource.svgaplayer.SVGAParser
import com.opensource.svgaplayer.SVGAVideoEntity
import java.io.File
import java.util.concurrent.ConcurrentHashMap

class GiftAdapter(
    private val list: List<GiftEntity>,
    private val onGiftClick: (GiftEntity) -> Unit
) : RecyclerView.Adapter<GiftAdapter.GiftViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GiftViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_gift, parent, false)
        return GiftViewHolder(view)
    }

    companion object {
        // 简单内存缓存，避免重复解析
        private val svgaFrameCache = ConcurrentHashMap<String, Bitmap?>()
    }

    override fun onBindViewHolder(holder: GiftViewHolder, position: Int) {
        val gift = list[position]
        holder.tvName.text = gift.title
        holder.tvPrice.text = "${gift.price}金币"
        // 本地资源映射
        val resId = when (gift.title) {
            "气球" -> R.drawable.gift_with_balloons
            "跑车" -> R.drawable.gift_car
            "旋转木马" -> R.drawable.gift_carousel
            "皇冠", "王冠" -> R.drawable.crown
            "爱心" -> R.drawable.heart_gift
            "火箭" -> R.drawable.rocket
            "玫瑰" -> R.drawable.flower_bouquet
            else -> R.drawable.ic_launcher_background
        }
        holder.ivIcon.setImageResource(resId)
        holder.itemView.setOnClickListener { onGiftClick(gift) }
    }

    override fun getItemCount() = list.size

    class GiftViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val ivIcon: ImageView = view.findViewById(R.id.iv_gift_icon)
        val tvName: TextView = view.findViewById(R.id.tv_gift_name)
        val tvPrice: TextView = view.findViewById(R.id.tv_gift_price)
    }
} 